package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	"strconv"
	_ "time"
)

type MemberMom struct {
	MemberMomId int    `json:"memberMomId" gorm:"type:int(11);primary_key"` //
	MemberId    int    `json:"memberId" gorm:"type:int(11);"`               //
	MomAge      string `json:"momAge" gorm:"type:varchar(32);"`             // 达人年龄
	MomLevel    string `json:"momLevel" gorm:"type:varchar(32);"`           // 佣金级别

	MomOrgan      int     `json:"momOrgan" gorm:"type:int(11);"`            // 签约机构数
	MomCustomer   int     `json:"momCustomer" gorm:"type:int(11);"`         // 客户数
	MomNewer      int     `json:"momNewer" gorm:"type:int(11);"`            // 社区达人拓展数
	MomCommission float64 `json:"momCommission" gorm:"type:decimal(10,2);"` // 社区达人佣金zonge

	SuccessTime  string `json:"successTime" gorm:"type:varchar(32);"`  // 审核时间
	ApprovalTime string `json:"approvalTime" gorm:"type:varchar(32);"` // 申请时间
	MomPhone     string `json:"momPhone" gorm:"type:varchar(255);"`    // 社区达人 电话

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

func (MemberMom) TableName() string {
	return "member_mom"
}

// UpdateMomNums
func (e *MemberMom) UpdateMomNums(id int, nums int) (bool, error) {
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("member_id  = ?", id)

	l := strconv.Itoa(nums)
	err := table.Update("mom_newer", l).Error
	return true, err
}

// UpdateMomLevel
func (e *MemberMom) UpdateMomLevel(id int, level int) (bool, error) {
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("member_id  = ?", id)

	l := strconv.Itoa(level)
	err := table.Update("mom_level", l).Error
	return true, err
}

// 创建MemberMom
func (e *MemberMom) Create() (MemberMom, error) {
	var doc MemberMom
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// GetByMID
func (e *MemberMom) GetByMID(id int) (MemberMom, error) {
	var doc MemberMom
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("member_id = ?", id)
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberMom
func (e *MemberMom) Get() (MemberMom, error) {
	var doc MemberMom
	table := orm.Eloquent.Table(e.TableName())
	if e.MemberMomId != 0 {
		table = table.Where("member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.MomAge != "" {
		table = table.Where("mom_age = ?", e.MomAge)
	}
	if e.MomLevel != "" {
		table = table.Where("mom_level = ?", e.MomLevel)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberMom带分页
func (e *MemberMom) GetPage(pageSize int, pageIndex int) ([]MemberMom, int, error) {
	var doc []MemberMom

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.MemberMomId != 0 {
		table = table.Where("member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.MomAge != "" {
		table = table.Where("mom_age = ?", e.MomAge)
	}
	if e.MomLevel != "" {
		table = table.Where("mom_level = ?", e.MomLevel)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新MemberMom
func (e *MemberMom) Update(id int) (update MemberMom, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("member_mom_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除MemberMom
func (e *MemberMom) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("member_mom_id = ?", id).Delete(&MemberMom{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberMom) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("member_mom_id in (?)", id).Delete(&MemberMom{}).Error; err != nil {
		return
	}
	Result = true
	return
}
